package com.qianfeng.wuyang.himalaya.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.GravityCompat;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.widget.Toast;

import com.qianfeng.wuyang.himalaya.R;
import com.qianfeng.wuyang.himalaya.adapter.recommendadapter.TodayHottestAdapter;
import com.qianfeng.wuyang.himalaya.bean.recommendbean.HottestTopEntity;
import com.qianfeng.wuyang.himalaya.bean.recommendbean.TodayHottestCategoryEntity;
import com.qianfeng.wuyang.himalaya.callback.recommendcallback.TopHottestService;
import com.qianfeng.wuyang.himalaya.fragment.TodayHottestFragment;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class DiscoveryActivity extends AppCompatActivity implements Callback<HottestTopEntity> {

    private TabLayout tabLayout;
    private ViewPager viewpager;
    private TodayHottestAdapter adapter;

    private List<TodayHottestCategoryEntity> categoryList = new ArrayList<>();
    private String key;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_discovery);

//        initView();

        Intent intent = getIntent();
        key = intent.getStringExtra("key");


//        initFragment();

    }

    private void initFragment() {
        TodayHottestFragment fragment = new TodayHottestFragment();
        FragmentManager manager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = manager.beginTransaction();
        fragmentTransaction.add(R.id.drawer_container,fragment);


    }

    private void initView() {
//        tabLayout = (TabLayout) findViewById(R.id.tablayout_discoveryactivity);
//        viewpager = (ViewPager) findViewById(R.id.viewpager_discoveryactivity);
//        adapter = new TodayHottestAdapter(getSupportFragmentManager(),this,categoryList);
//        viewpager.setAdapter(adapter);
//        tabLayout.setupWithViewPager(viewpager);

        DrawerLayout drawerLayout = (DrawerLayout) findViewById(R.id.drawer_discoveryactivity);
        drawerLayout.openDrawer(GravityCompat.END);


        TopHottestService service = new Retrofit.Builder()
                .baseUrl("http://mobile.ximalaya.com/")
                .addConverterFactory(GsonConverterFactory.create())
                .build()
                .create(TopHottestService.class);
        service.getTopHottest("android",key,1,20,2).enqueue(this);
    }


    @Override
    public void onResponse(Call<HottestTopEntity> call, Response<HottestTopEntity> response) {



        if (response.body()!=null) {
            categoryList.addAll(response.body().getCategories());
        }
//        adapter.notifyDataSetChanged();
    }

    @Override
    public void onFailure(Call<HottestTopEntity> call, Throwable t) {
        t.printStackTrace();
        Toast.makeText(this,t.getMessage(),Toast.LENGTH_SHORT).show();
    }
}
